﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MapEditor
{
    public partial class TerrainTile : UserControl
    {
        private int _imageIDLayer1 = -1;
        private int _imageIDLayer2 = -1;
        private int _xPos;
        private int _yPos;

        public int XPos
        {
            get { return _xPos; }
        }

        public int YPos
        {
            get { return _yPos; }
        }

        public void ToggleGrid()
        {
            if (PlotRect.Visibility == Visibility.Visible)
                PlotRect.Visibility = Visibility.Collapsed;
            else
                PlotRect.Visibility = Visibility.Visible;
        }

        public void ApplyOpacityMask()
        {
            Rectangle rect = Consts.PreviewOpacityRect.rect;

            TileImageLayer2.OpacityMask = rect.OpacityMask;
        }

        public void ShowLayer(Enums.Layer layer, bool show)
        {
            Image img = TileImageLayer1;

            if (layer == Enums.Layer.Layer2)
                img = TileImageLayer2;

            if(true == show)
                img.Visibility = Visibility.Visible;
            else
                img.Visibility = Visibility.Collapsed;
        }

        public int ImageIDLayer1
        {
            get { return _imageIDLayer1; }
            set
            {
                if (value == Consts.ImageNone)
                {
                    _imageIDLayer1 = value;
                    TileImageLayer1.Source = null;
                }
                else
                {
                    ImageResource imgResource = GameObjectMgr.GetMapImageByID(value);
                    if (null != imgResource)
                    {
                        _imageIDLayer1 = value;
                        TileImageLayer1.Source = Utils.LoadImage(imgResource.fileName);
                    }
                }
            }
        }

        public int ImageIDLayer2
        {
            get { return _imageIDLayer2; }
            set
            {
                if (value == Consts.ImageNone)
                {
                    _imageIDLayer2 = value;
                    TileImageLayer2.Source = null;
                }
                else
                {
                    ImageResource imgResource = GameObjectMgr.GetMapImageByID(value);
                    if (null != imgResource)
                    {
                        _imageIDLayer2 = value;
                        TileImageLayer2.Source = Utils.LoadImage(imgResource.fileName);
                    }
                }
            }
        }

        public TerrainTile(double xLoc, double yLoc, double tileRadius)
        {
            InitializeComponent();

            _xPos = (int)(xLoc / tileRadius);
            _yPos = (int)(yLoc / tileRadius);
            
            this.SetValue(Canvas.LeftProperty, xLoc);
            this.SetValue(Canvas.TopProperty, yLoc);

            this.Width = tileRadius;
            this.Height = tileRadius;

            PlotRect.Width = tileRadius;
            PlotRect.Height = tileRadius;
            
            TileImageLayer1.Width = tileRadius;
            TileImageLayer1.Height = tileRadius;


            TileImageLayer2.Width = tileRadius;
            TileImageLayer2.Height = tileRadius;

            TileXY.SetValue(Canvas.LeftProperty, (double)tileRadius / 2 - 10);
            TileXY.SetValue(Canvas.TopProperty, (double)tileRadius / 2 - 10);
        }

        public void DebugSetTileXY(string xy)
        {
            TileXY.Text = xy;
        }
    }
}
